package com.tutorial.webservices.saaj;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;

import com.tutorial.webservices.saaj.utils.HeadersParser;

/**
 * Servlet implementation class SaajServlet
 */
public class SaajServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private final HeadersParser headerParser;
    private MessageFactory messageFactory;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public SaajServlet() {
	super();
	this.headerParser = new HeadersParser();
	try {
	    this.messageFactory = MessageFactory.newInstance();
	} catch (SOAPException e) {
	    e.printStackTrace();
	}
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
	// Autogenerated
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
	    HttpServletResponse response) throws ServletException, IOException {
	this.doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
	    HttpServletResponse response) throws ServletException, IOException {
	this.headerParser.printHeaders(request);
	MimeHeaders mimeHeaders = this.headerParser.getMimeHeaders(request);
	InputStream is = request.getInputStream();
	OutputStream os = null;
	SOAPMessage message = null;
	SOAPMessage reply = null;
	try {
	    message = this.messageFactory.createMessage(mimeHeaders, is);
	    reply = onMessage(message);
	    if (reply == null) {
		response.setStatus(HttpServletResponse.SC_NO_CONTENT);
		return;
	    }
	    if (reply.saveRequired()) {
		reply.saveChanges();
	    }
	    response.setStatus(HttpServletResponse.SC_OK);
	    this.headerParser.putResponseHeaders(mimeHeaders, response);
	    os = response.getOutputStream();
	    reply.writeTo(os);
	    os.flush();
	} catch (SOAPException e) {
	    e.printStackTrace();
	} finally {
	    if (os != null) {
		os.close();
	    }
	}
    }

    /*
     * SOAPBody xml
     * 
     */
    private SOAPMessage onMessage(final SOAPMessage message)
	    throws SOAPException {
	SOAPMessage reply = this.messageFactory.createMessage();
	SOAPBody body = reply.getSOAPPart().getEnvelope().getBody();
	QName qName = new QName("http://www.tutorial.com", "reply", "m"); //$NON-NL-1$  //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
	body.addBodyElement(qName)
		.addChildElement(new QName("reply-message")).addTextNode("OK"); //$NON-NLS-1$ //$NON-NLS-2$
	return reply;
    }
}
